package main

import "fmt"

type fund func()

type Person struct {
	name string
	age  int
}

func main() {
	s2 := "AnonymousFunction"
	for i2 := range s2 {

		fmt.Println(i2)
	}
	getById()
	result := add(10, 5)
	fmt.Println(result)

	result1 := proxy(10, add)
	fmt.Println(result1)

	for3Return, b, i := addFor3Return(20)
	fmt.Println(for3Return, b, i)

	orderId := getFunction()(123)
	fmt.Println(orderId)

	// 匿名函数
	f := func(n1 int, n2 int) int {
		return n1 + n2
	}
	result3 := f(1, 2)
	fmt.Println(result3)

	result4 := func(n1 int, n2 int) int {
		return n1 + n2
	}(2, 3)
	fmt.Println(result4)

	result5 := getAnonymousFunction()(1)
	fmt.Println(result5)

	// 闭包
	var functionList []func()
	for i := 0; i < 5; i++ {
		function := func(arg int) func() {
			return func() {
				fmt.Println(arg)
			}
		}
		functionList = append(functionList, function(i))
	}

	functionList[0]()
	functionList[1]()
	functionList[2]()

	doSomething()

	person := getPerson()
	fmt.Println(person.name)

	p := Person{"ttao", 25}
	fmt.Println(p.getPerson())

	a, s := getString()
	fmt.Println(a, s)

}

func getById() {
	fmt.Println("getById")
}

func add(num1 int, num2 int) int {
	return num1 + num2
}

func proxy(data int, exec func(int, int) int) int {
	result := exec(data, 10)
	fmt.Println(result)
	return result
}

func getFunction() intToString {
	return getByOrderId
}

func addFor3Return(num1 int) (int, bool, int) {
	return num1 + 100, true, 0
}

func getByOrderId(orderId int) string {
	return "getByOrderId"
}

func getAnonymousFunction() func(n1 int) string {
	return func(n1 int) string {
		return "AnonymousFunction"
	}
}

func doSomething() {

	defer closeSomethingFor2()

	defer closeSomethingFor1()

	fmt.Println("before doSomething")

	fmt.Println("after doSomething")

}

func closeSomethingFor1() {
	fmt.Println("closeSomethingFor1")

}

func closeSomethingFor2() {
	fmt.Println("closeSomethingFor2")
}

func getPerson() Person {
	return Person{"ttao", 18}
}
func (this *Person) getPerson() int {
	return this.age
}

func getString() (a int, b string) {
	a = 1
	b = "apple"
	return a, b
}

type intToString func(int) string
